<template>
    <div class="page-bottom-bar">
        <div class="flex-item tip-message">预估报价：<span>￥</span><span>{{ $store.state.shop.orderPrice }}</span></div>
        <div class="flex-item price-protect">
            <van-switch v-model="pricePredictState" active-color="#00CC00" inactive-color="#eeeeee" size="14px" />
            <span>价格保护</span>
        </div>
        <div class="flex-item">
            <span @click="eventClickShopAddOrder" class="bar-button bar-button-green">立即下单</span>
        </div>
    </div>
</template>

<style lang="scss" scoped>
    @import "../../../libs/styles/bottom-fixed-bar.scss";

    .tip-message {
        padding-left: 1rem;
        text-align: left;

        span {
            color: #FF0000;
        }

        span:first-child {
            font-size: 1.3rem;
        }

        span:last-child {
            font-size: 1.9rem;
        }
    }
</style>

<script>
    import { Switch } from 'vant';

    export default {
        components: {
            [Switch.name]: Switch
        },
        computed: {
            pricePredictState: {
                get() {
                    return this.$store.state.shop.status.predictPrice;
                },
                set() {
                    this.$store.commit('shop/setElementValue', [
                        {
                            key: 'status.predictPrice', value: !this.$store.state.shop.status.predictPrice
                        }
                    ]);
                }
            }
        },
        methods: {
            buildFormaDataset(mode) {
                var result = [];

                for(let index of this.$store.state.shop.selectItems) {
                    let x = this.$store.state.shop.carts[index];

                    if(mode = 'predict') {
                        result.push({
                            id: x.id,
                            garbageid: x.idList,
                            // weighting_num: x.weight || x.number,
                            // weighting_method: x.weight ? 0 : 1
							danweiming: x.unit,
							weighting_num: x.number
                        });
                    } else {
                        result.push({
                            garbageid: x.idList,
                            // weighting_num: x.weight || x.number,
                            // weighting_method: x.weight ? 0 : 1
							danweiming: x.unit,
							weighting_num: x.number
                        });
                    }
                }

                return result;
            },
            eventClickShopAddOrder() {
                if(this.$store.state.shop.selectItems.length == 0) {
                    this.$toast('请选择要下单的选项');
                    return;
                }

                var url = '/api/home/order/shopadd',
                    params = {
                        stock_id: 1,
                        garbagelist: JSON.stringify(this.buildFormaDataset()),
                        is_tracing: (this.$store.state.shop.selectMode == 'scan') ? 2 : 1,
                        order_sn: (this.$store.state.shop.selectMode == 'scan') ? this.$store.state.shop.order : '',
                        is_protect: Number(this.pricePredictState),
                        token: this.$store.state.options.user.token
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    this.$store.commit('shop/resetShopCartsDataset', []);
                    this.$router.push({
                        path: '/shop/withdraw', query: {
                            ordernumber: res.data.ordernumber
                        }
                    });
                });
            }
        }
    }
</script>
